#ifndef __SNDA_COMM_RWLOCK_H__
#define __SNDA_COMM_RWLOCK_H__

#include <pthread.h>

enum LOCK_PRI
{
	DEFAULT		= 0x00,
	WRITE_PRI 	= 0x01,
	READ_PRI 	= 0x02,
};

class CRWLock
{
	public:
		CRWLock(LOCK_PRI pri=DEFAULT)
		{
			pthread_rwlockattr_t attr;
			pthread_rwlockattr_init(&attr);
			if(pri == READ_PRI)
			{
				pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_READER_NP);
			}
			else if(pri == WRITE_PRI)
			{
				pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
			}
			pthread_rwlock_init(&m_rwlock, &attr);
		}
		
    	~CRWLock()
		{
			pthread_rwlock_destroy(&m_rwlock);
		}

		int rdlock()
		{
			return pthread_rwlock_rdlock(&m_rwlock);
		};

		int wrlock() 
		{			
			return pthread_rwlock_wrlock(&m_rwlock);
		}

		int unlock()
		{			
			return pthread_rwlock_unlock(&m_rwlock);
		}
    
	private:
    	pthread_rwlock_t	m_rwlock;
};

#endif
